#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QJsonArray>
#include <memory>
#include <random>
#include <volumetool.h>
#include <QMediaPlayer>
#include <QAudioOutput>
#include <QPropertyAnimation>
#include <QSqlDatabase>
#include "qmediaplaylist.h"
#include "musiclist.h"
#include "commonpage.h"
#include "lrcpage.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

enum PlayBackMode{
    Loop,
    Random,
    CurrentItemInLoop
};

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    void initUi(); //处理widget页面

    void connectTheSlots(); //处理槽函数链接

    void initPlayer(); //初始化歌曲播放相关对象

    void initSQLite(); //初始化sqlite数据库

    void initMusicList();

    QJsonArray& randomPiction(std::mt19937&);

    void setPlayBackMode(int);

    int getPlayBackMode();

    void setRecentMusic(int index);

    void setPlayerSilient(bool);

    void setPlayerVolume(int);

    void readFromSQLite();

protected:
    void mouseMoveEvent(QMouseEvent* event); //处理鼠标左键调整窗口位置

    void mousePressEvent(QMouseEvent* event);

private slots:
    void setStackWidgetIndex(int id);

    void updateLikeMusicAndPage(bool, const QString&);

    void onPlayMusic();

    void playUp();

    void playDown();

    void onPlayBackModeClicked();

    void onPlayAll(PageType);

    void playAllOnCommonPage(CommonPage*, int);

    void playClickedMusic(CommonPage*, int);

    void autoSwitch(int);

    void updateMusicInfo();

private:
    Ui::Widget *ui;

    QPoint dragPosition;

    std::shared_ptr<QJsonArray> objArray;

    VolumeTool* volumeTool;

    MusicList musicList; //程序用来组织歌曲文件的类

    QMediaPlayer* player;

    QAudioOutput* output;

    QMediaPlaylist* playlist;

    CommonPage* currentPage;

    QPropertyAnimation* lrcAnimation;

    QString currentLrcFilePath;

    int currentIndex;

    int playBackMode = Loop;

    int position;//当前播放位置

    int duration;//当前歌曲总时长

    LrcPage* lrcPage;

    QSqlDatabase sqlite;

    bool isDrag = false;
};
#endif // WIDGET_H
